package com.calvin.study.model.base;

import com.alibaba.fastjson.JSON;

import lombok.Data;

@Data
public class ResponseEntity {

	private boolean isOk; // 请求是否处理成功

	private int code; // 请求响应状态码（200、400、500）

	private String message; // 请求结果描述信息

	private Object data; // 请求结果数据（通常用于查询操作）

	private ResponseEntity() {
	}

	public static ResponseEntity error() {
		ResponseEntity resultBean = new ResponseEntity();
		resultBean.setOk(false);
		resultBean.setCode(9999);
		resultBean.setMessage("系统错误，请联系管理员！");
		return resultBean;
	}

	public static ResponseEntity error(String message) {
		ResponseEntity resultBean = new ResponseEntity();
		resultBean.setOk(false);
		resultBean.setCode(9999);
		resultBean.setMessage(message);
		return resultBean;
	}

	// 请求出现异常时的响应数据封装
	public static ResponseEntity error(int code, String message) {
		ResponseEntity resultBean = new ResponseEntity();
		resultBean.setOk(false);
		resultBean.setCode(code);
		resultBean.setMessage(message);
		return resultBean;
	}

	// 请求成功的响应，不带查询数据（用于删除、修改、新增接口）
	public static ResponseEntity success() {
		ResponseEntity responseEntity = new ResponseEntity();
		responseEntity.setOk(true);
		responseEntity.setCode(200);
		responseEntity.setMessage("请求响应成功!");
		return responseEntity;
	}

	// 请求成功的响应，带有查询数据（用于数据查询接口）
	public static ResponseEntity success(Object obj) {
		ResponseEntity responseEntity = new ResponseEntity();
		responseEntity.setOk(true);
		responseEntity.setCode(200);
		responseEntity.setMessage("请求响应成功!");
		responseEntity.setData(obj);
		return responseEntity;
	}

	// 请求成功的响应，带有查询数据（用于数据查询接口）
	public static ResponseEntity success(Object obj, String message) {
		ResponseEntity responseEntity = new ResponseEntity();
		responseEntity.setOk(true);
		responseEntity.setCode(200);
		responseEntity.setMessage(message);
		responseEntity.setData(obj);
		return responseEntity;
	}

	public String toJson() {
		return JSON.toJSONString(this);
	}

}
